﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CustomerArchives_AddVIP : System.Web.UI.Page, ICallbackEventHandler
{
    string result = "";
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["isorder"] != null)
        {
            HiddenField1.Value = "1";
            if (new BLL.SYSPARAM().GetAutoCreateCustomerAccountConfig())
            {
                hidIsAutoCreateCustomerAccount.Value = "1";
                txtAccounts.Enabled = false;
                Label1.Text = "账号自动生成";                
            }
            else
            {
                hidIsAutoCreateCustomerAccount.Value = "0";
            }
            if (new BLL.SYSPARAM().GetAutoCreateAccountPasswordConfig())
            {
                hidAutoCreateAccountPassword.Value = "1";
                Password1.Enabled = false;
                Password2.Enabled = false;
            }
            else
            {
                hidAutoCreateAccountPassword.Value = "0";
            }
            Select1.DataSource = new BLL.ORDERFROM().GetAllList();
            Select1.DataTextField = "OF_NAME";
            Select1.DataValueField = "OF_ACODE";
            Select1.DataBind();
            
        }
    }

    public void RaiseCallbackEvent(string eventArgument)
    {
        //可以根据传递的参数不同，调用不同的处理逻辑 
        try
        {
            string key = System.Configuration.ConfigurationSettings.AppSettings["CODE"].ToString();
            string sddm = System.Configuration.ConfigurationSettings.AppSettings["ckdm"].ToString();
            string[] tmp = eventArgument.Split('|');
            string accounts = tmp[0];
            string pwd = tmp[2];
            if (new BLL.SYSPARAM().GetAutoCreateAccountPasswordConfig())
            {
                pwd = "000000";
            }
            if (new BLL.SYSPARAM().GetPwdEncryptConfig()) //判断是否用密码加密,如果启用则加密密码
            {
                string jmkey = BLL.Common.GenerateKey();
                pwd = BLL.Common.EncryptString(pwd, jmkey);
            }
            string sex = tmp[1];
            string email = tmp[3];
            string userName = tmp[4];
            string province = tmp[5];
            string city = tmp[6];
            string area = tmp[7];
            string address = tmp[8];
            string postCode = tmp[9];
            string mobile = tmp[10];
            string phone = tmp[11];
            string Nike = tmp[12];
            string source = tmp[13];

            if (hidIsAutoCreateCustomerAccount.Value == "1")
            {
                string date = DateTime.Now.ToString();
                date = date.Replace("-", "");
                date = date.Replace(" ", "");
                date = date.Replace(":", "");
                accounts = source + date;
            }

            LX_Manage.ERP.Service erp = new LX_Manage.ERP.Service();
            string ReCommendCustomer = "";
            result = erp.Customer_Add(key, sddm, accounts, userName, sex, pwd, email, address, postCode, mobile, phone, Request.UserHostAddress, ReCommendCustomer, Nike, province, city, area);
            if (result == "1" || result == "2" || result == "3")
            {                
                return;
            }
            if (result != "-2" && result != "-1" && result != "0" && result != accounts)
            {
                if (province != "-1" && address != "")
                {
                    //写入地址信息
                    Model.V_CUSTOMER_ADDRESS custAddress = new Model.V_CUSTOMER_ADDRESS();
                    custAddress.UserId = result.Split('|')[0];
                    custAddress.Receiver = userName;
                    custAddress.Provice = province;
                    custAddress.City = city;
                    custAddress.Area = area;
                    custAddress.Address = FilterInput(address);
                    custAddress.PostDM = postCode;
                    custAddress.Tel = phone;
                    custAddress.Mobile = mobile;
                    custAddress.IsDel = "0";
                    custAddress.AddTime = DateTime.Now;
                    new BLL.V_CUSTOMER_ADDRESS().Add(custAddress);
                }

                Model.V_VIPSET vvps = new BLL.V_VIPSET().GetModelByGKDM(accounts);
                Model.V_CUSTOMER vc = new BLL.V_CUSTOMER().GetModel(accounts);
                result = vvps.GKDM + "," + vvps.ZK.ToString() + "," + vvps.MC + "," + vc.BYZD5;               
            }

        }
        catch
        {
            result = "0";
        }
    }
 
    public string GetCallbackResult()
    {
        return result;
    }
    #region 过滤危险字符
    /// <summary>
    /// 过滤危险字符
    /// </summary>
    /// <param name="input">需要过滤的字符</param>
    /// <returns>过滤后的字符</returns>
    public static string FilterInput(string input)
    {
        return input.Replace("'", "’").Replace("\"", "“").Replace("-", "－").Replace("=", "＝").Replace("<", "＜").Replace(">", "＞").Replace(",", "，").Replace("(", "（").Replace(")", "）").Replace("%", "").Replace('"', '\"').Replace("\n", "<br/>").Replace("\\", "＼");
    }
    #endregion
}
